package com.job.servicejob.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.job.commonutils.JwtUtils;
import com.job.servicebase.exceptionHandler.JobException;
import com.job.servicejob.entity.Admin;
import com.job.servicejob.entity.vo.RegisterVo;
import com.job.servicejob.mapper.AdminMapper;
import com.job.servicejob.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author myt
 * @since 2023-03-17
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {


    @Resource
    private AdminMapper adminMapper;
    //登录方法
    @Override
    public String login(Admin admin) {
//        System.out.println(admin);
        //获取登录手机号密码
        String phone = admin.getPhone();
        String password = admin.getPassword();
//        String username = admin.getUsername();

        //手机号和密码非空的判断
        if(StringUtils.isEmpty(phone)||StringUtils.isEmpty(password)){
            throw  new JobException(20001,"登录失败，手机号或密码为空");
        }
        //判断手机号是否正确
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        Admin phoneAdmin = baseMapper.selectOne(wrapper);
//        System.out.println(phoneAdmin)
        //手机号和密码非空的判断
        if(phoneAdmin == null){
            throw  new JobException(20001,"手机号不存在");
        }
        //判断密码

        if(!password.equals(phoneAdmin.getPassword())){
            throw  new JobException(20001,"密码错误");
        }

        System.out.println(phoneAdmin.getId());
        //登录成功，使用jwt
        String jwtToken =  JwtUtils.getJwtToken(phoneAdmin.getPhone(),phoneAdmin.getPassword(),phoneAdmin.getUsername(),phoneAdmin.getId());
//        System.out.println(jwtToken);
        return jwtToken;
    }

    //注册方法
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册数据

        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        //校验参数
        if( StringUtils.isEmpty(phone) ||
                StringUtils.isEmpty(password))
            throw new JobException(20001,"注册失败");

        //查询数据库中是否存在相同的手机号码
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new JobException(20001,"注册失败");
        }

        Admin admin = new Admin();
        admin.setPhone(phone);
        admin.setPassword(password);
        admin.setStatus(true);
        admin.setSex(true);
        admin.setUsername("student");
        admin.setName("学生");
        baseMapper.insert(admin);

    }

    @Override
    public List<Admin>  getByPhone(String phone) {
        List<Admin> admins = adminMapper.searchByPhoneNumber(phone);
        return admins;
    }


}
